package org.ws.edu.task3.xml;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.ws.edu.task3.model.Answer;
import org.ws.edu.task3.model.Question;
import org.ws.edu.task3.model.Test;
import org.ws.edu.task3.model.Type;

import com.thoughtworks.xstream.XStream;

public class Writer {

    public static void main(String[] args) {

        List<Question> questions = new ArrayList<Question>();
        List<Answer> answerForFirstQuestion = new ArrayList<Answer>();
        List<Answer> rightAnswerForFirstQuestion = new ArrayList<Answer>();
        List<Answer> answerForSecondQuestion = new ArrayList<Answer>();
        List<Answer> rightAnswerForSecondQuestion = new ArrayList<Answer>();
        HashMap<Integer, String> result = new HashMap<Integer, String>();

        Answer a1 = new Answer("First answer", 1.0);
        Answer a2 = new Answer("Second answer", 1.0);
        Answer a3 = new Answer("Third answer", 1.0);
        Answer a4 = new Answer("Fourth answer", 1.0);

        answerForFirstQuestion.add(a1);
        rightAnswerForFirstQuestion.add(a2);
        answerForSecondQuestion.add(a3);
        rightAnswerForSecondQuestion.add(a4);

        Question q1 = new Question("The first question", answerForFirstQuestion, rightAnswerForFirstQuestion);
        Question q2 = new Question("The second question", answerForSecondQuestion, rightAnswerForSecondQuestion);

        questions.add(q1);
        questions.add(q2);

        result.put(1, "First result");
        result.put(3, "Second result");
        result.put(5, "Third result");

        Test t = new Test();

        t.setName("First Test");
        t.setQuestions(questions);
        t.setType(Type.SCALES);
        t.setResult(result);

        XStream xs = new XStream();

        xs.alias("test", Test.class);
        xs.alias("question", Question.class);
        xs.alias("answer", Answer.class);

        xs.addImplicitCollection(Test.class, "questions");
        xs.addImplicitCollection(Question.class, "allAnswers");

        xs.useAttributeFor(Answer.class, "factor");
        xs.useAttributeFor(Answer.class, "name");
        xs.useAttributeFor(Question.class, "name");
        xs.useAttributeFor(Test.class, "name");

        try {
            FileOutputStream fs = new FileOutputStream("./tests/object.xml");
            xs.toXML(t, fs);
        } catch (FileNotFoundException e1) {
            e1.printStackTrace();
        }
    }
}
